﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Specialized;

namespace JoyxyAnalyzer.Utils
{
    public class IniFileHelper
    {
        #region Members
        /// <summary>
        /// 
        /// </summary>
        private string _iniFilePath = string.Empty;
        public string IniFilePath
        {
            get { return _iniFilePath; }
            set { _iniFilePath = value; }
        }
        #endregion Members

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="iniFile"></param>
        public IniFileHelper(string iniFile)
        {
            if (!File.Exists(iniFile))
            {
                FileHelper.createFile(iniFile);
            }

            this._iniFilePath = iniFile;
        }

        #endregion Constructors

        #region Methods

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
　　　　private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Write(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this._iniFilePath);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string ReadValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder(65535);
            GetPrivateProfileString(section, key, "", temp, 65535, this._iniFilePath);
            return temp.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public StringCollection ReadSection(string section)
        {
            StringCollection SectionList = new StringCollection();
            byte[] Buffer = new byte[65535];
            int bufLen = GetPrivateProfileString(section, null, null, Buffer, Buffer.GetUpperBound(0), this._iniFilePath);
            GetStringsFromBuffer(Buffer, bufLen, SectionList);

            return SectionList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Buffer"></param>
        /// <param name="bufLen"></param>
        /// <param name="Strings"></param>
        private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
        {
            Strings.Clear();
            if (bufLen != 0)
            {
                int start = 0;
                for (int i = 0; i < bufLen; i++)
                {
                    if ((Buffer[i] == 0) && ((i - start) > 0))
                    {
                        String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
                        Strings.Add(s);
                        start = i + 1;
                    }
                }
            }
        }


        #endregion
    }
}
